package com.wpx.fileSys.dao.impl;


import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.wpx.fileSys.dao.FileDao;
import com.wpx.fileSys.dao.FindDao;
import com.wpx.fileSys.entity.FileList;
import com.wpx.fileSys.utils.FileOperation;
import com.wpx.fileSys.utils.JdbcUtils;

public class FileDaoImpl implements FileDao {
	
//	static String filePath="/Users/jianghao/Documents/server/";
//	static String filePath="C://web//";

	@Override
	public String[] findFiles(String user_name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean updateFiles(String user_name) {
		// TODO Auto-generated method stub
		File file = new File(FileOperation.filePath+user_name);
		File[] fs = file.listFiles();
		List<File> list=new ArrayList<File>();
        for (File f : fs) {
            if (!f.isDirectory()) {
            	String fileName=f.getName();
                String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
                
                if (prefix.equals("txt")) {
                	list.add(f);
				}
			}
        }
        
        List<FileList> fileLists=insertFiles(list,user_name);
        if (!fileLists.isEmpty()) {
        	insertData(fileLists);
			System.out.println("列表更新成功");
			return true;
		}else {
			System.out.println("列表更新失败");
		}
        
		return false;
	}
	
	private List<FileList> insertFiles(List<File> files,String username) {
		
		String sql = "insert into Files(file_name, file_dir,file_time,file_type) "
				+ "values(?,?,?,?);";
		List<FileList> fileLists=new ArrayList<FileList>();
		Connection connection = JdbcUtils.getConnection();
		try {
			connection.setAutoCommit(false);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("关闭AutoCommit失败");
		}
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
			
			for(File f : files){
				preparedStatement.setString(1, f.getName());
				preparedStatement.setString(2, username);
				long time=f.lastModified();
				SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				preparedStatement.setString(3, dateFormat.format(time));
				
				String fileType=FileOperation.fileType(f.getName());
				preparedStatement.setString(4, fileType);
				preparedStatement.addBatch();
				
				FileList fileList=new FileList();
				fileList.setFile_name(f.getName());
				fileList.setFile_dir(username);
				fileList.setFile_time(dateFormat.format(time));
				fileList.setFile_type(fileType);
				fileLists.add(fileList);
			}

			preparedStatement.executeBatch();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("error 列表插入失败");
		} finally {
			JdbcUtils.close(null, preparedStatement, connection);
		}
		return fileLists;
	}
	
	private void insertData(List<FileList> fileLists) {
		
		for (FileList fileList : fileLists) {
			FileOperation.insertFileDatas(fileList);
		}
	}

	@Override
	public Boolean deleteFiles(String user_name) {
		// TODO Auto-generated method stub
		String sql_j = "delete data_j from Data_j left join Files on file_dir = j_username where file_dir=?";
		String sql_yys = "delete data_yys from data_yys left join Files on file_dir = yys_username where file_dir=?";
		String sql_yyssr = "delete data_yyssr from data_yyssr left join Files on file_dir = yysSR_username where file_dir=?";
		String sql_yyszh = "delete data_yyszh from data_yyszh left join Files on file_dir = yysZH_username where file_dir=?";
		String sql_z = "delete data_z,files from data_z left join Files on file_dir = z_username where file_dir=?";
		String sql_files = "delete files from Files where file_dir=?";

		List<String> sqls = new ArrayList<>();
		sqls.add(sql_j);
		sqls.add(sql_yys);
		sqls.add(sql_yyssr);
		sqls.add(sql_yyszh);
		sqls.add(sql_z);
		sqls.add(sql_files);
		
		Connection connection = JdbcUtils.getConnection();
		PreparedStatement preparedStatement = null;
		try {
			connection.setAutoCommit(false);
			
			for (String sqlString : sqls) {
				preparedStatement = connection.prepareStatement(sqlString);
				preparedStatement.setString(1, user_name);
				preparedStatement.addBatch();
			}
			preparedStatement.executeBatch();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("error 删除失败");
			return false;
		} finally {
			JdbcUtils.close(null, preparedStatement, connection);
		}
		return true;
	}




}
